﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities;
using BLL.BLL;
using Entities.IRepository;
using DAL.Admin;

namespace WindowsForms
{
    
    public partial class FormThemNhanVien : Form
    {
        public static NhanVien dataNv;
        public static DiaChi dataDc;
        public INhanVienRepository nv = new BLTNhanVienRepository();
        public IBoPhanRepository bp = new BLTBoPhanRepository();
      
        public IDiaChiRepository dchi = new BLTDiaChiRepositoy();
        string mabophan;
        public FormThemNhanVien()
        {
            InitializeComponent();
        }

        private void btluunhanvien_Click(object sender, EventArgs e)
        {

        }

        private void cbbophan_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        private void GanChoControl(NhanVien nhanvien, DiaChi dc)
        {
            if (nhanvien != null)
            {
                mtbmanv.Text = nhanvien.MaNhanVien;
                mtbmanv.ReadOnly = true;
                tbtennhanvien.Text = nhanvien.TenNhanVien;
                tbsodienthoai.Text = nhanvien.SoDienThoai;
                tbemail.Text = nhanvien.Email;
                //dtngaysinh.Value = nhanvien.NamSinh;
                //if (nhanvien.GioiTinh == "Nam")
                //    radnam.Checked = true;
                //else radnu.Checked = true;
                for (int i = 1; i < cbbophan.Items.Count; i++)
                {
                    if ((cbbophan.Items[i] as BoPhan).MaBoPhan == nhanvien.MaBoPhan)
                        cbbophan.SelectedIndex = i;
                }


            }

            if (dc != null)
            {
                tbsonha1.Text = dc.SoNha;
                tbtenduongnhv.Text = dc.TenDuong;
                tbquanhuyen.Text = dc.QuanHuyen;
                tbthanhpho.Text = dc.TinhTP;
            }
        }

        private void btluu_Click(object sender, EventArgs e)
        {
            NhanVien nhanvien = new NhanVien();
            DiaChi dc = new DiaChi();

            nhanvien.MaNhanVien = mtbmanv.Text;
            if (!String.IsNullOrWhiteSpace(mtbmanv.Text))
            {
                nhanvien.TenNhanVien = tbtennhanvien.Text;
                nhanvien.SoDienThoai = tbsodienthoai.Text;
                nhanvien.Email = tbemail.Text;
                nhanvien.MaBoPhan = mabophan;
                nhanvien.NamSinh = dtngaysinh.Value;

                //nhanvien.GioiTinh = radnam.Checked ? "Nam" : "Nữ";


                dc.MaDiaChi = mtbmanv.Text;
                dc.SoNha = tbsonha1.Text;
                dc.TenDuong = tbtenduongnhv.Text;
                dc.QuanHuyen = tbquanhuyen.Text;
                dc.TinhTP = tbthanhpho.Text;
                if (!QuanLiNhanVien.KiemTra(mtbmanv.Text, nv.LayTatCa()))
                {


                  bool kt=  nv.ThemNhanVien(nhanvien);
                  bool kt1=  dchi.ThemDiaChi(dc);
                    if(kt1 && kt)
                        MessageBox.Show("Successful");
                }
                else
                {
                    DialogResult rs = MessageBox.Show("Nhân Viên Đã Có Bạn Muốn Cập Nhật Không", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (rs == DialogResult.Yes)
                    {
                        dchi.CapNhatDiaChi(dc);
                        nv.CapNhatNhanVien(nhanvien);

                    }
                }

            }
            else MessageBox.Show("Nhập Mã Nhân Viên Lại", "Thông Báo Cho Người Dùng");
        }

        private void cbbophan_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (cbbophan.SelectedIndex > 0)
                mabophan = (cbbophan.SelectedItem as BoPhan).MaBoPhan;
        }

        private void FormThemNhanVien_Load(object sender, EventArgs e)
        {
            QuanLiNhanVien.LoadCbBoPhan(cbbophan, bp.LayTatCa());
            GanChoControl(dataNv, dataDc);
         
        }

        private void btthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
